// pages/adminHome/adminHome.js
//获取当前日期
import util from '../../utils/util';
const DATE = util.formatDate(new Date())

const db = wx.cloud.database().collection('resultList')
const db2 = wx.cloud.database().collection('ticketList');
const db3 = wx.cloud.database().collection('userList');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        ticketList: [],
        userList: [],
        admin: null,
        // user:null,
        btninfo: "抢号",
        resultList: [],
        DATE: '',
        sTime: "",
        eTime: "",
        id: '',
        show: false,
        count: null,
        countSum: null,
        des: null,
        title: null,
        countSumValue: null,
        desValue: null,
        titleValue: null,
        show: false,
        //输入用户信息
        name: "",
        no: "",
        tel: '',
        temp: null
    },

    //获取选号名单
    getResultList(e) {
        db.orderBy('_id', 'desc').get({
            success: (res) => {
                console.log("当前选号数据：");
                console.log(res.data);
                this.setData({
                    resultList: res.data
                })
            }
        })
    },

    onPullDownRefresh() {
        wx.showToast({
            title: '刷新数据',
            icon: 'loading'
        })
        this.getResultList()
        this.getTicketList()
        wx.stopPullDownRefresh({
            success: (res) => {
                wx.showToast({
                    title: '刷新成功',
                    icon: 'success'
                })
            },
        })
    },


    //获取相关输入
    getId(e) {
        this.setData({
            id: e.detail
        })
        var id = this.data.id
        console.log("编号：" + id);
    },
    getTitle(e) {
        this.setData({
            title: e.detail
        })
        var title = this.data.title
        console.log("业务名称：" + title);
    },
    getCountSum(e) {
        this.setData({
            countSum: e.detail
        })
        var countSum = this.data.countSum
        var count = this.data.countSum
        console.log("放号量：" + count);
    },
    getDes(e) {
        this.setData({
            des: e.detail
        })
        var des = this.data.des
        console.log("业务详情：" + des);
    },

    // 时分的事件方法
    time(e) {
        console.log("开始时期：" + e.detail.sTime)
        console.log("结束时期：" + e.detail.eTime)
        console.log(e);
        this.setData({
            sTime: e.detail.sTime,
            eTime: e.detail.eTime
        })
    },
    //发布业务
    launch() {
        var title = this.data.title
        var countSum = this.data.countSum
        var count = this.data.countSum
        var des = this.data.des
        var sTime = this.data.sTime
        var eTime = this.data.eTime
        var id = this.data.id

        //判断id是否重复
        var t = this.data.ticketList
        var flag = 0
        for (let i = 0; i < t.length; i++) {
            if (id == t[i].id) {
                flag = 1
                break
            }
        }

        if (id == '' || title == '' || countSum == '' || count == '' || des == '' || sTime == '') {
            wx.showToast({
                title: '输入不能为空',
                icon: 'error'
            })
        } else if (flag == 1) {
            wx.showToast({
                title: '编号已存在',
                icon: 'error'
            })
        } else {
            db2.add({
                data: {
                    count: count,
                    countSum: countSum,
                    sTime: sTime,
                    eTime: eTime,
                    id: id,
                    des: des,
                    title: title,
                    checked: 0
                },
                success: (res) => {
                    wx.showToast({
                        title: '发布成功!',
                        icon: 'success'
                    })
                    console.log(res);
                    this.setData({
                        countSum: '',
                        des: '',
                        title: '',
                        date: '',
                        sTime: '',
                        eTime: '',
                        id: '',
                    })
                    console.log("清空输入");
                    this.onLoad()
                }
            })
        }

    },

    //选号

    //获取抢号列表
    getTicketList(e) {
        db2.orderBy('_id', 'desc').get({
            success: (res) => {
                console.log("当前可抢号列表：");
                console.log(res.data);
                this.setData({
                    ticketList: res.data
                })
            }
        })
    },

    //获取用户列表
    getUserList(e) {
        db3.get({
            success: (res) => {
                console.log("用户列表：");
                console.log(res.data);
                this.setData({
                    userList: res.data
                })
            }
        })
    },

    //帮助抢号
    //管理员操作，无视日期限制
    showPopup(e) {
        this.setData({
            show: true
        });
        var t = e.currentTarget.dataset.data
        console.log(t);
        this.setData({
            temp: t
        })
    },

    onClose() {
        this.setData({
            show: false
        });
    },

    //获取输入姓名
    getNameInput(e) {
        this.setData({
            name: e.detail
        })
        var name = this.data.name
        console.log("name is : " + name)
    },
    //获取输入工号
    getNoInput(e) {
        this.setData({
            no: e.detail
        })
        var no = this.data.no
        console.log("no is : " + no);
    },
    //获取电话
    getTelInput(e) {
        this.setData({
            tel: e.detail
        })
        var tel = this.data.tel
        console.log("tel is : " + tel);
    },

    onConfirm() {

        //判断用户是否存在
        var user = this.data.userList
        var no = this.data.no
        var name = this.data.name
        var tel = this.data.tel
        var flag = 0
        for (let i = 0; i < user.length; i++) {
            if (user[i].no == no && user[i].name == name && user[i].tel == tel) {
                flag = 1
                console.log('用户匹配成功');
            }
        }

        if (flag == 0) {
            console.log("用户匹配失败");
            wx.showToast({
              title: '用户匹配失败',
              icon:'error'
            })
        } else if (flag == 1) {
            var data = this.data.temp
            if (data.count == 0) {
                wx.showToast({
                    title: '号已抢空!',
                    icon: "error"
                })
            } else {
                console.log("执行抢号");
                var no = this.data.no
                var name = this.data.name
                var tel = this.data.tel
                var title = data.title
                var id = data.id
                var ticketNo = data.countSum - data.count + 1
                db.add({
                    data: {
                        name: name,
                        no: no,
                        ticketNo: ticketNo,
                        title: title,
                        tel:tel,
                        id:id
                    },
                    success: (res) => {
                        wx.showToast({
                            title: '抢号成功!',
                        })
                        console.log("抢号成功!");
                        console.log(res);
                        this.setData({
                            show:false,
                            no:'',
                            name:'',
                            tel:''
                        })
                        //抢号成功后更新数据
                        var count = data.count
                        console.log(count);
                        db2.where({
                            _id: data._id
                        }).update({
                            data: {
                                count: count - 1
                            },
                            success: (res) => {
                                console.log("数据更新：");
                                console.log(res);
                                this.onLoad()
                            }
                        })
                    }
                })
            }
        }
    },

    onLoad() {
        this.getUserList()
        this.getTicketList()
        this.getResultList()

        this.setData({
            admin: wx.getStorageSync('admin'),
            DATE: DATE
        })
        var admin = this.data.admin
        console.log("当前时间：" + DATE);
        console.log("管理员信息：");
        console.log(admin);

    }
})